package com.csx.queue;

/**
 * @author 陈胤训
 * <p>
 * create: 2019-05-05 16:11
 **/
public class Solution {



    /**
     * 删除链表中等于给定值 value 的所有节点。
     *
     * 示例:
     *
     * 输入: 1->2->6->3->4->5->6, value = 6
     * 输出: 1->2->3->4->5
     *
     * @param head
     * @param val
     * @return
     */
    public ListNode removeElements(ListNode head, int val) {
        while(head.val == val && head != null){
            ListNode delNode = head;
             head = head.next;
            delNode.next = null;
        }
        if (head == null){
            return null;
        }
        ListNode prev = head;
        while (prev.next != null){
            if (prev.next.val == val){
                ListNode delNode = prev.next;
                prev.next = delNode.next;
                delNode.next = null;
            }else{
                prev = prev.next;
            }
        }
        return head;
    }

    /**
     * 添加虚拟表头的解决方法
     * @param head
     * @param val
     * @return
     */
    public ListNode removeElements2(ListNode head, int val) {
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;

        ListNode prev = dummyHead;
        while (prev.next != null){
            if (prev.next.val == val){
                prev.next = prev.next.next;
            }else{
                prev = prev.next;
            }
        }
        return dummyHead.next;
    }

    /**
     * 递归解决方法
     * @param head
     * @param val
     * @return
     */
    public ListNode removeElements3(ListNode head, int val) {
        if (head == null){
            return null;
        }
        head.next = removeElements3(head.next, val);
        /*if (head.value == value){
            return head.next;
        }else{
            return head;
        }*/
        return head.val == val ? head.next : head;
    }

    /**
     * 递归实现 累加
     * @param arr
     * @return
     */
    public static int sum(int[] arr){
        return sum(arr,0);
    }

    /**
     * 计算arr[l....n]这个区间所有数字之和
     * @param arr
     * @param l
     * @return
     */
    private static int sum(int[] arr,int l){
        if (l == arr.length){
            return 0;
        }
        return arr[l] + sum(arr,l+1);
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3,6,4,5,6};
        System.out.println(sum(nums));
        ListNode head = new ListNode(nums);
        System.out.println(head);
        ListNode listNode = (new Solution()).removeElements3(head, 6);
//        ListNode listNode1 = (new Solution()).removeElements(head, 6);
        System.out.println(listNode);
//        System.out.println(listNode1);

    }

}
  class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
    ListNode(int[] arr) {
       if (arr == null || arr.length == 0){
           throw new IllegalArgumentException("ListNode构造函数 . arr 为空 ");
       }
       this.val = arr[0];
       ListNode cur = this;
        for (int i = 1; i < arr.length; i++) {
            cur.next = new ListNode(arr[i]);
            cur = cur.next;
        }
    }
      @Override
      public String toString(){
        StringBuilder builder = new StringBuilder();
        ListNode cur = this;
        while(cur != null){
            builder.append(cur.val + "->");
            cur = cur.next;
        }
        builder.append("NULL");
        return builder.toString();
      }
}